/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package percussiongenerator.gui;

import java.awt.Component;
import javax.swing.Box;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jfonia.connect5.basics.Observer;
import org.jfonia.model.QuantizerNode;
import org.jfonia.model.TempoNode;
import org.jfonia.model.quantize.BPMQuantizer;
import org.jfonia.model.quantize.NoteOnQuantizer;

/**
 * This panel is used to change the settings of the quantizer.
 * @author jannes
 */
public class QuantizerPanel extends JPanel implements Observer{
    protected QuantizerNode quantizerNode;
    protected TempoNode tempoNode;
    protected JCheckBox chbDetectBPM;

    public QuantizerPanel(QuantizerNode quantizerNode_, TempoNode tempoNode){
        quantizerNode = quantizerNode_;
        this.tempoNode = tempoNode;
        this.tempoNode.getBPMNode().addObserver(this);
        Box vBox = Box.createVerticalBox();
        this.add(vBox);        
        chbDetectBPM = new JCheckBox("Detect BPM");
        addLeftHBox(vBox,chbDetectBPM);
        if(quantizerNode.getQuantizer() instanceof NoteOnQuantizer){
            chbDetectBPM.setSelected(true);
        }else{
            chbDetectBPM.setSelected(false);
        }
        chbDetectBPM.addChangeListener(new ChangeListener() {
            public void stateChanged(ChangeEvent e) {
                if(chbDetectBPM.isSelected() && !(quantizerNode.getQuantizer() instanceof NoteOnQuantizer)){                    
                    quantizerNode.setQuantizer(new NoteOnQuantizer(quantizerNode.getQuantizer().getSmallestValue()));
                }else if(!chbDetectBPM.isSelected() && !(quantizerNode.getQuantizer() instanceof BPMQuantizer)){                    
                    quantizerNode.setQuantizer(new BPMQuantizer(quantizerNode.getQuantizer().getSmallestValue()));                    
                }
            }
        });
        JLabel lblSmallestInterval = new JLabel("Interval, whole note=192: ");
        JTextField txtSmallestInterval = new JTextField(4);
        addLeftHBox(vBox, lblSmallestInterval, txtSmallestInterval);
    }

    public final Box addLeftHBox(Box vBox, Component component){
        Box hBox = Box.createHorizontalBox();
        vBox.add(hBox);
        hBox.add(component);
        hBox.add(Box.createHorizontalGlue());
        return hBox;
    }

    public final Box addLeftHBox(Box vBox, Component component1, Component component2){
        Box hBox = Box.createHorizontalBox();
        vBox.add(hBox);
        hBox.add(component1);
        hBox.add(component2);
        hBox.add(Box.createHorizontalGlue());
        return hBox;
    }

    public void removeObservers(){
        tempoNode.getBPMNode().removeObserver(this);
    }

    public void onNotify(Object source) {        
        if(source == tempoNode.getBPMNode()){
            chbDetectBPM.setSelected(false);
        }
    }
}
